package com.lagou.jdbc03;

import com.lagou.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author wsl
 * @Date 2021/8/19 23:11
 * @Project_Name mysql
 */
public class JDBCTransaction {

    /**
     * JDBC 操作事务
     * @param args
     */
    public static void main(String[] args) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            //1.注册驱动
            con = JDBCUtils.getConnection();

            //2.开启事务
            con.setAutoCommit(false);

            //3.获取到 prepareStatement 执行两次更新操作
            //3.1 tom 账户 -500
            ps = con.prepareStatement("update account set money = money - ? where name = ?");
            ps.setDouble(1,500.0);
            ps.setString(2,"tom");
            ps.executeUpdate();

            //模拟tom转涨后 出现异常
            System.out.println(1/0);

            //3.2 jack 账户 +500
            ps = con.prepareStatement("update account set money = money + ? where name = ?");
            ps.setDouble(1,500.0);
            ps.setString(2,"jack");
            ps.executeUpdate();

            //4.正常情况下提交事务
            con.commit();
            System.out.println("转账成功！");

        } catch (SQLException e) {
            e.printStackTrace();
            try {
                //5.出现异常回滚事务
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            //6.关闭资源
            JDBCUtils.close(con,ps);
        }
    }
}
